knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(rmarkdown)
library(flexdashboard)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(magrittr)
## 
## 다음의 패키지를 부착합니다: 'magrittr'
## 
## The following object is masked from 'package:purrr':
## 
##     set_names
## 
## The following object is masked from 'package:tidyr':
## 
##     extract
library(DT)
library(plotly)
## 
## 다음의 패키지를 부착합니다: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout

서론

본 보고서는 iris 데이터셋을 대상으로 기본 통계량, 변수 간 상관관계, 그리고 간단한 선형 회귀 분석을 수행한 결과를 정리한 것입니다.
iris 데이터셋은 붓꽃(Iris)의 종(Species)별로 꽃받침(Sepal)과 꽃잎(Petal)의 길이 및 너비를 측정한 데이터로, 각 변수 간의 관계를 분석하기에 적합한 예제 데이터입니다.


데이터 탐색 및 기술 통계


data_name <- params$dataset
data <- get(data_name)

# iris 데이터셋인 경우에만 분석 코드 실행
if(data_name == "iris"){
  summary(data)
  plot(data)
} else {
  # iris 데이터셋이 아닐 경우: 아무 작업도 수행하지 않음
}


변수 간 상관관계 분석


if(data_name == "iris"){
  cor_matrix <- cor(data[, 1:4])
  round(cor_matrix, 2)
} else {
  # iris 데이터셋이 아닐 경우: 아무 작업도 수행하지 않음
}
##              Sepal.Length Sepal.Width Petal.Length Petal.Width
## Sepal.Length         1.00       -0.12         0.87        0.82
## Sepal.Width         -0.12        1.00        -0.43       -0.37
## Petal.Length         0.87       -0.43         1.00        0.96
## Petal.Width          0.82       -0.37         0.96        1.00


선형 회귀 분석


if(data_name == "iris"){
  model <- lm(Petal.Length ~ Sepal.Length, data = data)
  summary(model)
} else {
  # iris 데이터셋이 아닐 경우: 아무 작업도 수행하지 않음
}
## 
## Call:
## lm(formula = Petal.Length ~ Sepal.Length, data = data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.47747 -0.59072 -0.00668  0.60484  2.49512 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -7.10144    0.50666  -14.02   <2e-16 ***
## Sepal.Length  1.85843    0.08586   21.65   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.8678 on 148 degrees of freedom
## Multiple R-squared:   0.76,  Adjusted R-squared:  0.7583 
## F-statistic: 468.6 on 1 and 148 DF,  p-value: < 2.2e-16


시각화 및 대시보드


if(data_name == "iris"){
  # iris 데이터셋에서 모든 종을 선택한 후, 상위 50행만 사용
  selected_species <- unique(data$Species)
  filtered_data <- data %>% 
    filter(Species %in% selected_species) %>% 
    head(50)
  
  p <- ggplot(filtered_data, aes(x = Sepal.Length, y = Petal.Length, color = Species)) +
    geom_point(size = 3, alpha = 0.8) +
    labs(title = "Iris 데이터 산점도", x = "Sepal Length", y = "Petal Length") +
    theme_minimal()
  
  if (knitr::is_html_output()) {
    ggplotly(p)
  } else {
    p
  }
} else {
  # iris 데이터셋이 아닐 경우: 아무 작업도 수행하지 않음
}

if(data_name == "iris"){
  if (knitr::is_html_output()) {
    DT::datatable(filtered_data)
  } else {
    knitr::kable(filtered_data)
  }
} else {
  # iris 데이터셋이 아닐 경우: 아무 작업도 수행하지 않음
}


결론

분석 요약

  • 기술 통계 및 데이터 탐색:
    각 변수의 기본 통계량과 분포를 통해 iris 데이터셋의 전반적인 특성을 파악할 수 있었습니다.

  • 상관관계 분석:

    • 꽃받침 길이와 꽃잎 길이, 그리고 꽃받침 길이와 꽃잎 너비 사이에는 강한 양의 상관관계가 확인되었습니다.
    • 특히, 꽃잎 길이와 꽃잎 너비의 상관계수(0.96)는 두 변수 간의 매우 밀접한 관계를 나타냅니다.
  • 선형 회귀 분석:

    • 꽃받침 길이가 꽃잎 길이에 미치는 영향이 통계적으로 매우 유의미한 것으로 나타났습니다.
    • 꽃받침 길이가 1 단위 증가할 때마다 꽃잎 길이가 평균적으로 1.86 단위 증가하며, 모델의 R² 값은 약 0.76입니다.

의의 및 활용

  • 본 보고서는 iris 데이터셋의 특성을 종합적으로 분석함으로써, 변수들 간의 관계와 상호 작용을 심도 있게 이해하는 데 기여합니다.
  • 이러한 분석 결과는 향후 데이터 분석 및 모델링 연구에 유용한 기초 자료로 활용될 수 있습니다.